﻿
namespace HmmmIDE.Debug
{
    public class Methods
    {
        #region System Instructions
        public static void halt()
        {
            DebugService.PC = -1;
        }

        public static void nop()
        {
            DebugService.PC++;
        }
        public static void read(Register rX)
        {
            rX.Value = DebugService.Read();
            DebugService.PC++;
        }

        public static void write(Register rx)
        {
            DebugService.Write(rx.Value);
            DebugService.PC++;
        }
        #endregion

        #region Setting	Register Data
        public static void setn(Register rX, short n)
        {
            rX.Value = (short)n;
            DebugService.PC++;
        }

        public static void addn(Register rX, short n)
        {
            checked
            {
                rX.Value += (short)n;
                DebugService.PC++;
            }
        }

        public static void copy(Register rX, Register rY)
        {
            rX.Value = rY.Value;
            DebugService.PC++;
        }

        #endregion

        #region Arithmetic
        public static void add(Register rX, Register rY, Register rZ)
        {
            checked
            {
                rX.Value = (short)(rY.Value + rZ.Value);
                DebugService.PC++;
            }
        }

        public static void sub(Register rX, Register rY, Register rZ)
        {
            checked
            {
                rX.Value = (short)(rY.Value - rZ.Value);
                DebugService.PC++;
            }
        }

        public static void neg(Register rX, Register rY)
        {
            rX.Value = (short)(-rY.Value);
            DebugService.PC++;
        }

        public static void mul(Register rX, Register rY, Register rZ)
        {
            checked
            {
                rX.Value = (short)(rY.Value * rZ.Value);
                DebugService.PC++;
            }
        }

        public static void div(Register rX, Register rY, Register rZ)
        {
            rX.Value = (short)(rY.Value / rZ.Value);
            DebugService.PC++;
        }

        public static void mod(Register rX, Register rY, Register rZ)
        {
            rX.Value = (short)(rY.Value % rZ.Value);
            DebugService.PC++;
        }

        #endregion

        #region Jumps
        public static void jumpn(short n)
        {
            DebugService.PC = n;
        }

        public static void jumpr(Register rX)
        {
            DebugService.PC = rX.Value;
        }

        public static void jeqzn(Register rX, short n)
        {
            DebugService.PC = rX.Value == 0 ? n : DebugService.PC + 1;
        }

        public static void jnezn(Register rX, short n)
        {
            DebugService.PC = rX.Value != 0 ? n : DebugService.PC + 1;
        }

        public static void jgtzn(Register rX, short n)
        {
            DebugService.PC = rX.Value > 0 ? n : DebugService.PC + 1;
        }

        public static void jltzn(Register rX, short n)
        {
            DebugService.PC = rX.Value < 0 ? n : DebugService.PC + 1;
        }

        public static void calln(Register rX, short n)
        {
            rX.Value = (short)(DebugService.PC + 1);
            DebugService.PC = n;
        }
        #endregion

        #region Interacting with Memory (RAM)
        public static void loadn(Register rX, short n)
        {
            rX.Value = DebugService.Memory[n];
            DebugService.PC++;
        }

        public static void storen(Register rX, short n)
        {
            DebugService.Memory[n] = rX.Value ;
            DebugService.PC++;
        }

        public static void loadr(Register rX, Register rY)
        {
            rX.Value = DebugService.Memory[rY.Value];
            DebugService.PC++;
        }

        public static void storer(Register rX, Register rY)
        {
            DebugService.Memory[rY.Value] = rX.Value;
            DebugService.PC++;
        }
        #endregion
    }
}
